package view.user;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;


import entity.User;
import service.user.UserService;
import service.user.UserServiceImpl;
import utils.ComonUtil;
import view.LoginView;
import view.MainView;
import view.CommonView.CommonJPanel;

/**
 * @Author: Lin Hongwei
 * @LastEditors: Lin Hongwei
 * @email: 1365755396@qq.com
 * @CSDN: https://blog.csdn.net/weixin_45429409
 * @Description: 
 *
 */
public class UpPassView extends CommonJPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	JPasswordField passwordField, passwordField2;
	private static UserService userServiceImpl = new UserServiceImpl();
	public UpPassView() {
		super(TYPE_TRANSPARENT);
		this.setSize(MainView.instance.getWidth(),MainView.instance.getHeight());


		// 4. 创建输入框
		CommonJPanel fieldPanel = new CommonJPanel(CommonJPanel.TYPE_TRANSPARENT);
		// 行数 , 列数 ,水平 , 竖直 网格间隙
		fieldPanel.setLayout(new GridLayout(2, 2, 0, 30));
	
		add(fieldPanel, BorderLayout.CENTER);

		// 4.3 创建密码标签
		JLabel passwordLabel = new JLabel("密　码：");
		passwordLabel.setFont(new Font("微软雅黑", Font.BOLD, 20));
		fieldPanel.add(passwordLabel);
		// 4.4 创建密码输入框
		passwordField = new JPasswordField();
		passwordField.setPreferredSize(new Dimension(150, 40));
		passwordField.setFont(new Font("微软雅黑", Font.BOLD, 20));
		fieldPanel.add(passwordField);

		JLabel passwordLabel2 = new JLabel("确认密码：");
		passwordLabel2.setFont(new Font("微软雅黑", Font.BOLD, 20));
		fieldPanel.add(passwordLabel2);
		// 4.4 创建密码输入框
		passwordField2 = new JPasswordField();
		passwordField2.setPreferredSize(new Dimension(150, 40));
		passwordField2.setFont(new Font("微软雅黑", Font.BOLD, 20));
		fieldPanel.add(passwordField2);

		// 5. 创建按钮面板
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
		buttonPanel.setPreferredSize(new Dimension(MainView.instance.getWidth()-200, 40));
		buttonPanel.setOpaque(false);// 设置背景色为透明
		add(buttonPanel);
		// 5.1 创建登录按钮
		JButton loginBtn = new JButton("修改");
		loginBtn.setFont(new Font("微软雅黑", Font.BOLD, 15));
		buttonPanel.add(loginBtn);
		// 5.2 创建重置按钮
		JButton resetBtn = new JButton("重置");
		resetBtn.setFont(new Font("微软雅黑", Font.BOLD, 15));
		buttonPanel.add(resetBtn);

		// 5.3 监听登录按钮事件
		loginBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
			
				String pass = new String(passwordField.getPassword());
				String pass1 = new String(passwordField2.getPassword());
	
				if (ComonUtil.isEmpty(pass) || ComonUtil.isEmpty(pass1)) {
					JOptionPane.showMessageDialog(UpPassView.this, "密码不能为空！");
					return;
				}
				if (!pass.equals(pass1)) {
					JOptionPane.showMessageDialog(UpPassView.this, "密码不一致！");
					return;
				}
				
				
				User user = LoginView.getUser();
				user.setPassword(pass);
				String flag ="更新失败！";
				if(userServiceImpl.upUser(user)) {
					flag = "更新成功！";
				}
				JOptionPane.showMessageDialog(UpPassView.this, flag);

				return;

			}

		});

		resetBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				passwordField.setText("");
				passwordField2.setText("");
			}

		});
	}

}
